package com.example.springbootchapter09.controllor;

import com.example.springbootchapter09.service.EmailService;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class EmailController {
    @Autowired
    private EmailService emailService;
    @GetMapping("/sendPlainTextEmail")
    public String sendPlainTextEmail() {
        emailService.sendPlainTextEmail("recipient@example.com", "测试邮件", "这是一封纯文本测试邮件");
        return "纯文本邮件已发送";
    }
    @GetMapping("/sendAttachmentAndImageEmail")
    public String sendAttachmentAndImageEmail() {
        try {
            emailService.sendAttachmentAndImageEmail("recipient@example.com", "带附件和图片的邮件", "<html><body><img src='cid:imageId'></body></html>", "/path/to/attachment.pdf", "/path/to/image.jpg");
            return "带附件和图片的邮件已发送";
        } catch (MessagingException e) {
            e.printStackTrace();
            return "发送邮件出错";
        }
    }
    @GetMapping("/sendTemplateEmail")
    public String sendTemplateEmail() {
        try {
            emailService.sendTemplateEmail("recipient@example.com", "模板邮件");
            return "模板邮件已发送";
        } catch (MessagingException | IOException e) {
            e.printStackTrace();
            return "发送邮件出错";
        }
    }
}